Package org.javacommerce.google.handler.impl

Source Code of org.javacommerce.google.handler.impl.DefaultMerchantCalculationCallbackHandler

/**
*
*/
package org.javacommerce.google.handler.impl;

import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.javacommerce.core.config.JavaCommerce;
import org.javacommerce.google.handler.CalculationException;
import org.javacommerce.google.handler.CalculationHandler;

import com.google.checkout.AnonymousAddress;
import com.google.checkout.Item;
import com.google.checkout.Items;
import com.google.checkout.MerchantCalculationCallback;
import com.google.checkout.MerchantCalculationResults;
import com.google.checkout.Method;
import com.google.checkout.Result;
import com.google.checkout.Results;
import com.google.checkout.ShippingRate;
import com.google.checkout.TotalTax;

/**
* Default MerchantCalculcationCallback Handler.  Only supports tax and shipping calculcations
* based on a configuration file.  If no values are present in the configuration, 0's are used.
* @author Michael Blanton (mike@mikeblanton.com)
*/
public class DefaultMerchantCalculationCallbackHandler implements CalculationHandler {
 
  private static final Log LOG = LogFactory.getLog(DefaultMerchantCalculationCallbackHandler.class);
 
  public static final String DEFAULT_CURRENCY = "USD";
 
  public static final String CONFIG_GOOGLE_CALC = "google.calculation";
  public static final String CONFIG_GOOGLE_CALC_TAX = new StringBuffer(CONFIG_GOOGLE_CALC).append(".tax").toString();
  public static final String CONFIG_GOOGLE_CALC_TAX_DEFAULT = new StringBuffer(CONFIG_GOOGLE_CALC_TAX).append(".default").toString();
  public static final BigDecimal DEFAULT_TAX = new BigDecimal("0.00");
 
  public static final String CONFIG_GOOGLE_CALC_SHIPPING = new StringBuffer(CONFIG_GOOGLE_CALC).append(".shipping").toString();
  public static final String CONFIG_GOOGLE_CALC_SHIPPING_DEFAULT = new StringBuffer(CONFIG_GOOGLE_CALC_SHIPPING).append(".default").toString();
  public static final BigDecimal DEFAULT_SHIPPING = new BigDecimal("0.00");
  public static final String DOT_DEFAULT = ".default";

  public MerchantCalculationResults performCalculation(MerchantCalculationCallback _callback) throws CalculationException {
    MerchantCalculationResults mcResults = new MerchantCalculationResults();
    Results results = new Results();
    mcResults.setResults(results);
    boolean calcTax = _callback.getCalculate().getTax();
    boolean calcShipping = false;
    if (_callback.getCalculate().getShipping() != null) {
      calcShipping = true;
    }
    BigDecimal orderTotal = calculateOrderTotal(_callback.getShoppingCart().getItems());
    if (LOG.isDebugEnabled()) {
      LOG.debug("Order Total is [" + orderTotal + "]");
    }
    BigDecimal defaultTax = JavaCommerce.getConfiguration().getBigDecimal(CONFIG_GOOGLE_CALC_TAX_DEFAULT, DEFAULT_TAX);
    BigDecimal defaultShipping = JavaCommerce.getConfiguration().getBigDecimal(CONFIG_GOOGLE_CALC_SHIPPING_DEFAULT, DEFAULT_SHIPPING);
   
    // Addresses is always passed according to the XSD.
    for (int i = 0; i < _callback.getCalculate().getAddresses().getAnonymousAddressCount(); i++) {
      AnonymousAddress address = _callback.getCalculate().getAddresses().getAnonymousAddress(i);
      TotalTax totalTax = null;
      if (calcTax) {
        // Get the tax rate
        BigDecimal taxRate = JavaCommerce.getConfiguration().getBigDecimal(new StringBuffer(CONFIG_GOOGLE_CALC_TAX).append(".").append(address.getRegion()).toString(), defaultTax);
        // Calculate Tax
        BigDecimal tax = taxRate.multiply(orderTotal);
        tax = tax.setScale(2, BigDecimal.ROUND_UP);
        if (LOG.isDebugEnabled()) {
          LOG.debug("Calculated tax of [" + tax + "] with rate of [" + taxRate + "]");
        }
        totalTax = new TotalTax();
        totalTax.setContent(tax);
        totalTax.setCurrency(DEFAULT_CURRENCY);
      }
      if (calcShipping) {
        for (int j = 0; j < _callback.getCalculate().getShipping().getMethodCount(); j++) {
          Result result = new Result();
          result.setAddressId(address.getId());
          result.setShippable(true);
          Method method = _callback.getCalculate().getShipping().getMethod(j);
          result.setShippingName(method.getName());
          // Load Shipping, read order:
          // 1. google.calculation.shipping.{method name}.{address region}
          // 2. google.calculation.shipping.{method name}.default
          // 3. google.calculation.shipping.default
          BigDecimal shipping = JavaCommerce.getConfiguration().getBigDecimal(new StringBuffer(CONFIG_GOOGLE_CALC_SHIPPING).append(".").append(method.getName()).append(".").append(address.getRegion()).toString(), JavaCommerce.getConfiguration().getBigDecimal(new StringBuffer(CONFIG_GOOGLE_CALC_SHIPPING).append(".").append(method.getName()).append(DOT_DEFAULT).toString(), defaultShipping));
          shipping = shipping.setScale(2, BigDecimal.ROUND_UP);
          if (LOG.isDebugEnabled()) {
            LOG.debug("Calculated shipping of [" + shipping + "] for [" + method.getName() + "]");
          }
          ShippingRate rate = new ShippingRate();
          rate.setContent(shipping);
          rate.setCurrency(DEFAULT_CURRENCY);
          result.setShippingRate(rate);
          if (calcTax) {
            result.setTotalTax(totalTax);
          }
          results.addResult(result);
        }
      }
      else if (calcTax) {
        Result result = new Result();
        result.setAddressId(address.getId());
        result.setTotalTax(totalTax);
        results.addResult(result);
      }
    }
    return mcResults;
  }

  private BigDecimal calculateOrderTotal(Items _items) {
    BigDecimal total = new BigDecimal("0").setScale(2);
    for (int i = 0; i < _items.getItemCount(); i++) {
      Item item = _items.getItem(i);
      total = total.add(item.getUnitPrice().getContent().multiply(new BigDecimal(item.getQuantity())));
    }
    total = total.setScale(2, BigDecimal.ROUND_UP);
    return total;
  }
 

}
TOP

Related Classes of org.javacommerce.google.handler.impl.DefaultMerchantCalculationCallbackHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.